package com.example.drug.controller;

import com.example.drug.domain.Inventory;
import com.example.drug.service.InventoryService;
import com.example.drug.util.AjaxResult;
import com.example.drug.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存Controller
 *
 * @author [你的名字]
 * @date [具体日期]
 */
@RestController
@RequestMapping("/api/inventory")
@Api(tags = "药店库存管理609余函池")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 获取全部库存列表
     */
    @ApiOperation("获得全部库存列表")
    @GetMapping("/inventorylist")
    public AjaxResult getAllInventoryList() {
        return AjaxResult.success(inventoryService.selectAllInventory());
    }

    /**
     * 根据条件查询库存列表（此处以药品编号为例，可按需扩展更多条件）
     */
    @ApiOperation("根据条件查询库存列表")
    @ApiImplicitParam(name = "drugId", value = "药品编号",
            dataType = "long", dataTypeClass = Long.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "drugId", required = false) Long drugId) {
        List<Inventory> list = inventoryService.selectInventoryByParam(drugId);
        return AjaxResult.success(list);
    }

    /**
     * 获取库存详细信息
     */
    @ApiOperation("获取库存详细信息")
    @ApiImplicitParam(name = "id", value = "库存ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Inventory inventory = inventoryService.selectInventoryById(id);
        if (inventory == null || inventory.getInventoryId() == null) {
            throw new BizException(404, "库存 " + id + " 不存在");
        }
        return AjaxResult.success(inventory);
    }

    /**
     * 新增库存记录
     */
    @ApiOperation("新增库存记录")
    @PostMapping
    public AjaxResult add(@RequestBody Inventory inventory) {
        return AjaxResult.success(inventoryService.insertInventory(inventory));
    }

    /**
     * 修改库存记录
     */
    @ApiOperation("修改库存记录")
    @PutMapping
    public AjaxResult edit(@RequestBody Inventory inventory) {
        return AjaxResult.success(inventoryService.updateInventory(inventory));
    }

    /**
     * 删除库存记录
     */
    @ApiOperation("删除库存记录")
    @ApiImplicitParam(name = "id", value = "库存ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(inventoryService.deleteInventoryById(id));
    }
}